﻿using System;
using System.Web.UI.WebControls;

namespace Core.Validation
{
    public class ValidationError : BaseValidator, IEquatable<ValidationError>
    {
        public ValidationError(string property, string errorMessage, object onObject)
        {
            this.Property = property;
            this.ErrorMessage = errorMessage;
            this.Object = onObject;
            IsValid = false;
        }

        public ValidationError(string propertyName, string errorMessage)
            : this(propertyName, errorMessage, null)
        {
        }

        public string Property { get; set; }

        public object Object { get; private set; }
        

        /// <summary>
        /// When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.
        /// </summary>
        /// <returns>
        /// true if the value in the input control is valid; otherwise, false.
        /// </returns>
        protected override bool EvaluateIsValid()
        {
            return false;
        }

        protected override bool ControlPropertiesValid()
        {
            return true; 
        }

        #region Implementation of IEquatable<ValidationError>

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ValidationError other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return other.IsValid.Equals(IsValid) && Equals(other.ErrorMessage, ErrorMessage);
        }

        #endregion
    }

}
